/**
 * 
 */
package com.wobuhekele.domain;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;

import com.wobuhekele.core.AbstractDownExecute;
import com.wobuhekele.util.StreamUtils;

import java.util.ArrayList;
import java.util.concurrent.Callable;

/**
 * <b>功能说明:</b><p>
</p>
 * @author xchen
 * @CreateDate 2017-3-27 - 上午10:57:32 
 * @Encoding UTF-8
 * @Version 1.0
 */
public class QuotoTask implements Callable<DownResultBean> {
	private int threadExecuteIndex;
	private int singleThreadExecuteSize;
	private InputStream userInputStream;
	private int startIndex ; 
	private int endIndex;
	private final ThreadLocal<String> threadLocal=new ThreadLocal<String>();
	private final String idString;
	private int local;
	
	private String getID() {
		return idString;
	}
	/**
	 * @param startIndex
	 * @param endIndex
	 * @param userInputStream2
	 * @param singleThreadExecuteSize 
	 */
	public QuotoTask(int startIndex, int endIndex,
			InputStream userInputStream2,int local, int singleThreadExecuteSize) {
		this.singleThreadExecuteSize = singleThreadExecuteSize;
		this.startIndex = startIndex;
		this.endIndex = endIndex;
		this.idString=startIndex+"-"+endIndex;
		this.local = local;
		this.userInputStream = userInputStream2;
//		System.out.println("QuotoTask()==>startIndex : " + startIndex + "  endIndex : " + endIndex );
	}

	/**
	 * @return
	 * @throws Exception
	 * @Date 2017-3-22 - 下午3:16:42
	 */
	@Override
	public DownResultBean call() throws Exception {
		
		DownResultBean downResultBean = new DownResultBean();
		downResultBean.setStartIndex(startIndex);
		//  从 开始(threadExecuteIndex*singleThreadExecuteSize) 结束位置(threadExecuteIndex*singleThreadExecuteSize)
		System.out.println("call()==>第"+local+"个从 " + startIndex + "开始" +
				endIndex+"结束, 应下载大小:"+(endIndex-startIndex));
		//threadLocal.set(startIndex+"-"+endIndex);
		byte [] buffer =  new byte[1024*1024*32];
		byte [] returnBuffer = new byte[]{} ;
		ArrayList<File> tempFiles = new ArrayList<File>();
		File tempFile = File.createTempFile("local 为" + local +"的临时文件", "temp");
//		FileOutputStream fis = new FileOutputStream(tempFile);
		tempFiles.add(tempFile);
		int b ;
		int begin =0;
		int cacheLimit = 1024 * 1024 * 5;
		int end =endIndex - startIndex; //总共需要读取的总数
//		userInputStream.skip(startIndex);
		while( (b = userInputStream.read(buffer,0,buffer.length)) != -1){
			cacheLimit = cacheLimit + b;
			returnBuffer = StreamUtils.addBufferByte(buffer, 0, b ,returnBuffer);
		}
		
		int skipTemp = 0;
		skipTemp = local* singleThreadExecuteSize;
		if(local == (AbstractDownExecute.threadSize -1)){//最后一个
			skipTemp = endIndex - singleThreadExecuteSize ; 
		}
			
		File resultFile = new File("C:\\Users\\chenxiong\\Desktop\\myeclipse测试"/*+new Date().getTime()*/+".exe");
		RandomAccessFile accessFile = new RandomAccessFile(resultFile, "rw");
		System.out.println("跳过的距离 skipTemp "+skipTemp );
		accessFile.seek(skipTemp);
		accessFile.write(returnBuffer,0,returnBuffer.length);
		
		
			//大于 设置的缓存大小  读写到临时文件
//			FileOutputStream fis = new FileOutputStream(tempFile,true);
//			fis.write(returnBuffer);
//			fis.close();
//			returnBuffer = new byte[]{}; //初始化
//			end = end - begin;
//			begin = 0;
		
		System.out.println("call()==> local= " + local +"线程读取了 "+ returnBuffer.length +" 个字节" + "临时文件的数量"+ tempFiles.size());
		
		
		downResultBean.setResultByte(returnBuffer);
		downResultBean.setLocal(local);
		downResultBean.setLocalTempFiles(tempFiles);
//		downResultBean.setStartIndex(startIndex);
		return downResultBean;
	}
	
	
	public void zz(){

		
//		DownResultBean downResultBean = new DownResultBean();
//		downResultBean.setStartIndex(startIndex);
//		//  从 开始(threadExecuteIndex*singleThreadExecuteSize) 结束位置(threadExecuteIndex*singleThreadExecuteSize)
//		System.out.println("call()==>第"+local+"个从 " + startIndex + "开始" +
//				endIndex+"结束, 应下载大小:"+(endIndex-startIndex));
//		//threadLocal.set(startIndex+"-"+endIndex);
//		byte [] buffer =  new byte[4096];
//		byte [] returnBuffer = new byte[]{} ;
//		ArrayList<File> tempFiles = new ArrayList<File>();
//		File tempFile = File.createTempFile("local 为" + local +"的临时文件", "temp");
////		FileOutputStream fis = new FileOutputStream(tempFile);
//		tempFiles.add(tempFile);
//		int b ;
//		int begin =0;
//		int cacheLimit = 1024 * 1024 * 5;
//		int end =endIndex - startIndex; //总共需要读取的总数
//		userInputStream.skip(startIndex);
//		while( (b = userInputStream.read(buffer,0,buffer.length)) != -1){
//			cacheLimit = cacheLimit + b;
//			
//			if((begin +b) > end){//endIndex 807692  begin 807936
//				b = -(begin -end);
//				returnBuffer = StreamUtils.addBufferByte(buffer, 0, b ,returnBuffer);
//				
////				File tempFile = File.createTempFile("local 为" + local +"的临时文件", "temp");
////				tempFiles.add(tempFile);
//				FileOutputStream fis = new FileOutputStream(tempFile,true);
//				fis.write(returnBuffer);
//				fis.close(); 
//				returnBuffer = new byte[]{}; //初始化
//				
//				
//				break;
//			}else{
//				// 如果到了末尾是会被读取的
//				startIndex = startIndex + b;
//				begin = begin + b ;
//				returnBuffer = StreamUtils.addBufferByte(buffer, 0, b ,returnBuffer);
//				if((endIndex - startIndex )< buffer.length){
//					System.out.println("call()==>(endIndex - startIndex )< buffer.length");
//					//表示下一次 会读到最后 
//					FileOutputStream fis = new FileOutputStream(tempFile,true);
//					fis.write(returnBuffer);
//					fis.close();
//					returnBuffer = new byte[]{}; //初始化
//					end = end - begin;
//					begin = 0;
//				}
//			}
//			
//			if(returnBuffer.length > cacheLimit){
//				//大于 设置的缓存大小  读写到临时文件
//				FileOutputStream fis = new FileOutputStream(tempFile,true);
//				fis.write(returnBuffer);
//				fis.close();
//				returnBuffer = new byte[]{}; //初始化
//				end = end - begin;
//				begin = 0;
//			}
//			
////			if(begin > 1024 * 1024 * 10){//10m 写入到临时文件中
//////				File tempFile = File.createTempFile("local 为" + local +"的临时文件", "temp");
//////				tempFiles.add(tempFile);
//////				FileOutputStream fis = new FileOutputStream(tempFile);
////				fis.write(returnBuffer);
////				fis.close();
////				returnBuffer = new byte[]{}; //初始化
////				begin = 0;
////				end = end -begin;
////			}
//			
//			
//		}
////		fis.close();
//		if(returnBuffer.length >0){
////			returnBuffer = addBufferByte(buffer, 0, b ,returnBuffer);
////			File tempFile = File.createTempFile("local 为" + local +"的临时文件", "temp");
////			tempFiles.add(tempFile);
//			FileOutputStream fis = new FileOutputStream(tempFile,true);
//			fis.write(returnBuffer);
//			fis.close();
//			returnBuffer = new byte[]{}; //初始化
////			System.out.println("------ returnBuffer.length >0 -- 大小为" + returnBuffer.length);
//		}
//		
//		System.out.println("call()==> local= " + local +"线程读取了 "+ returnBuffer.length +" 个字节" + "临时文件的数量"+ tempFiles.size());
//		
//		
//		downResultBean.setResultByte(returnBuffer);
//		downResultBean.setLocal(local);
//		downResultBean.setLocalTempFiles(tempFiles);
////		downResultBean.setStartIndex(startIndex);
//		return downResultBean;
	
	}
	
}
